<?php

namespace App\Console\Commands\Mybank;

use App\Kuafu\Mybank\Model\MybankOrderRecordModel;
use App\Kuafu\Mybank\Model\MybankWalletRecordModel;
use App\Kuafu\Mybank\Service\MybankOrderWithholdService;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Pay\Model\RefundStreamModel;
use Illuminate\Console\Command;

class InitMybankCashierWalletRecordCommand extends Command
{
    protected $signature = 'mybank:cashierWalletRecord';

    protected $description = '网商小程序支付-生成支付成功但未记录到钱包记录表的数据';

    public function handle()
    {

        PayStreamModel::query()
                      ->where('payChannel', PayStreamModel::PAY_CHANNEL_MYBANK_CASHIER_PAY)//网商小程序支付
                      ->where('payStatus', PayStreamModel::PAY_STATUS_1)//已支付
                      ->where('sysType', PayStreamModel::SYS_TYPE_ROS)//商家app订货
                      ->orderBy('createdAt')
                      ->chunk(100, function ($payStreamModel) {
                          foreach ($payStreamModel as $v) {
                              if (!$v->thirdPayNo) {
                                  continue;
                              }

                              dump('thirdPayNo:' . $v->thirdPayNo . ' shopId:' . $v->shopId);

                              // 查询关联支出的记录
                              $record = MybankWalletRecordModel::query()
                                                               ->where('transId', $v->thirdPayNo)
                                                               ->first();
                              if (!$record) {

                                  // 钱包添加记录
                                  MybankOrderWithholdService::walletRecordAdd(
                                      '',
                                      MybankWalletRecordModel::PAY_TRANS_TYPE,
                                      $v->payPrice,
                                      0,
                                      $v->shopId,
                                      0,
                                      MybankWalletRecordModel::RECORD_ID_TYPE_CASHIER_PAY,
                                      MybankWalletRecordModel::PLATFORM_TYPE_MYBANK,
                                      MybankOrderRecordModel::WALLET_TYPE_SHOW_TYPE_TEXT[MybankOrderRecordModel::TYPE_KFOS_ORDER],
                                      $v->thirdPayNo,
                                      '（网商结算台）',
                                      $v->paySuccessTime,
                                      MybankWalletRecordModel::SUCCESS_STATUS,
                                      MybankWalletRecordModel::SHOW_RECORD
                                  );

                                  dump('thirdPayNo:' . $v->thirdPayNo . ' 订货支出-添加到钱包记录');
                              }

                              // 查询是否有退款记录
                              $refundStream = RefundStreamModel::query()
                                                               ->where('refundStatus', RefundStreamModel::REFUND_STATUS_OK)//退款成功
                                                               ->where('payNo', $v->payNo)//原我方支付订单流水号
                                                               ->first();
                              if ($refundStream) {

                                  // 查询关联退款的记录
                                  $record = MybankWalletRecordModel::query()
                                                                   ->where('transId', $refundStream->thirdRefundNo)
                                                                   ->first();
                                  if (!$record) {

                                      // 钱包添加记录
                                      MybankOrderWithholdService::walletRecordAdd(
                                          '',
                                          MybankWalletRecordModel::EARNING_TRANS_TYPE,
                                          $refundStream->refundPrice,
                                          0,
                                          $refundStream->shopId,
                                          0,
                                          MybankWalletRecordModel::RECORD_ID_TYPE_CASHIER_PAY,
                                          MybankWalletRecordModel::PLATFORM_TYPE_MYBANK,
                                          MybankOrderRecordModel::WALLET_TYPE_SHOW_TYPE_TEXT[MybankOrderRecordModel::TYPE_KFOS_ORDER],
                                          $refundStream->thirdRefundNo,
                                          '（网商结算台）',
                                          $refundStream->refundSuccessTime,
                                          MybankWalletRecordModel::SUCCESS_STATUS,
                                          MybankWalletRecordModel::SHOW_RECORD
                                      );

                                      dump('thirdRefundNo:' . $refundStream->thirdRefundNo . ' 订货退款-添加到钱包记录');
                                  }
                              }
                          }
                      });

    }
}
